package com.zj.joke.db;

/**
 * Created by zhangjian on 13-12-6.
 */

import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;
import com.activeandroid.query.Delete;
import com.activeandroid.query.From;
import com.activeandroid.query.Select;

import java.util.ArrayList;
import java.util.List;

@Table(name = "DBStoreItem")
public class DBStoreItem extends Model {

	@Column(name = "category")
	public String category;

	@Column(name = "text")
	public String text;

	@Column(name = "title")
	public String title;

	@Column(name = "url")
	public String url;

	public static void deleteDBStoreItem(String category, String title) {
		From localFrom = new Delete().from(DBStoreItem.class);
		localFrom.where("category=? and title = ?", category, title).execute();
	}

	public static int getDBStoreItemSize(String category) {
		return new Select().from(DBStoreItem.class)
				.where("category=?", category).execute().size();
	}

	public static List<DBStoreItem> getDBStoreItems(String category) {
		return new Select().from(DBStoreItem.class)
				.where("category=?", category).execute();
	}

	public static List<DBStoreItem> getDBStoreItems(String category, int count) {
		List<DBStoreItem> localList = new Select().from(DBStoreItem.class)
				.where("category=?", category).execute();
		if (localList != null && count > localList.size()) {
			count = localList.size();
		}
		ArrayList<DBStoreItem> newList = new ArrayList<DBStoreItem>();
		if (localList != null) {
			for (int i = 0; i < count; i++) {
				DBStoreItem localDBStoreItem = (DBStoreItem) localList.get(i);
				newList.add(localDBStoreItem);
			}
		}
		return newList;
	}

}